export function checkPhoneNumber(str: string): [boolean, string | undefined] {
  if (str.length !== 11) {
    return [false, "手机号不为11位，请重新输入"];
  }
  if (
    str[0] !== "1" ||
    ["0", "1", "2"].includes(str[1]) ||
    /[^0-9]/.test(str)
  ) {
    return [false, "输入有误，请重新输入"];
  }
  return [true, undefined];
}

export function checkPwd(str: string): [boolean, string | undefined] {
  if (str.length < 8) {
    return [false, "密码长度不能小于8位"];
  }
  if (/[0-9]/.test(str) && /[a-zA-Z]/.test(str) && /[^0-9a-zA-Z]/.test(str)) {
    return [true, undefined];
  }
  return [false, "密码不符合规则，应包含数字、字母、符号，请重新设置"];
}

export function checkIdCard(str: string): [boolean, string | undefined] {
  // 身份证号码长度不为18
  if (str.length !== 18) {
    return [false, "身份证长度应为18位"];
  }
  // 身份证号码中出现非数字字符
  if (!/^\d{17}(\d|x)$/i.test(str)) {
    return [false, "身份证不号合法，请检查"];
  }
  // 身份证号码出生日期超出范围或含有非法字符
  var year = parseInt(str.substr(6, 4), 10);
  var month = parseInt(str.substr(10, 2), 10);
  var day = parseInt(str.substr(12, 2), 10);
  var date = new Date(year, month - 1, day);
  if (
    date.getFullYear() !== year ||
    date.getMonth() + 1 !== month ||
    date.getDate() !== day
  ) {
    return [false, "身份证号不合法"];
  }
  // 身份证号码校验位错误
  var sum = 0;
  var wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  var lastCode = "10x98765432";
  for (var i = 0; i < 17; i++) {
    sum += parseInt(str.charAt(i), 10) * wi[i];
  }
  var mod = sum % 11;
  if (str.charAt(17).toLowerCase() !== lastCode.charAt(mod)) {
    return [false, "身份证号不合法"];
  }
  // 身份证号码校验通过
  return [true, undefined];
}
